<?php

namespace app\models;

use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use app\common\Functions;


class Order extends ActiveRecord
{

    public $qCount;   // 本月个人的全部订单
    public $qMyCount; // 全部待我处理的订单
    public $custName;
    public $prodName;

    public $parentProdName;
    public $attrcode;
    public $province;
    public $areas;
    public $city;
    public $prodInstAttrSpec;
    public $prodAttrInstCode;
    public $localAttrsInsts;
    public $staffName;
    public $orderTypeName;
    public $resumesTypeID;
    public $need_area;
    public $accNbr;
    public $color;

    public function rules()
    {
        return [
            ['id', 'integer'],
            ['resumesTypeID', 'integer'],
            ['region_id', 'integer'],
            ['reason', 'integer'],
            ['remark', 'required', 'message' => '请务必填写备注信息.'],
            ['order_number', 'string'],
            ['input_time', 'validateProdAttr'],//空函数，可跳过验证
            ['state', 'integer'],

            ['attrSpecs', 'validateProdAttr'],
            ['localAttrsInsts', 'validateProdAttr'],
            ['custName', 'string'],
            ['prodName', 'string'],
            ['order_type_id', 'integer'],
            ['remark', 'string'],
            ['object_id', 'integer'],
            ['object_type_id', 'integer'],
            ['staff_id', 'integer'],

        ];

    }

    public function validateProdAttr($attribute, $params)
    {
//        return true;
    }

    public function setAccNbr($accNbr = null)
    {
        return $this->accNbr = $accNbr;
    }

    public function getAccNbr()
    {
        return $this->accNbr;
    }

    public function getResumesTypeID()
    {
        return $this->resumesTypeID;
    }

    public function getOrderTypeName()
    {
        return $this->orderTypeName;
    }

    public function getStaffName()
    {
        return $this->staffName;
    }

    public function getCustName()
    {
        return $this->custName;
    }

    public function getProdName()
    {
        return $this->prodName;
    }

    public function getAttrcode()
    {
        return $this->attrcode;
    }

    public function getProdAttrInstCode()
    {
        return $this->prodAttrInstCode;
    }

    public function getProvince()
    {
        return $this->province;
    }

    public function getCity()
    {
        return $this->city;
    }

    public function getArea()
    {
        return $this->areas;
    }

    public function getColor()
    {
        return $this->color;
    }





    public function getStaffs()
    {
        return $this->hasOne(Staff::className(), ['user_id' => 'staff_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getUsers()
    {
        return $this->hasOne(Staff::className(), ['user_id' => 'create_staff_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getModifyUsers()
    {
        return $this->hasOne(User::className(), ['id' => 'modify_staff_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getDealUsers()
    {
        return $this->hasOne(Staff::className(), ['user_id' => 'staff_id'] );
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }





    public function getCusts()
    {
        return $this->hasOne(Cust::className(), ['id' => 'object_id']);
       // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }



    /* 获取房源信息*/
    public function getProdInsts()
    {

        return $this->hasOne(ProdInst::className(), ['id' => 'object_id']);
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    /* 获取楼盘信息*/
    public function getParentProdInsts()
    {

        return $this->hasOne(ProdInst::className(), ['id' => 'parent_id'])->viaTable(ProdInst::tableName() . 'as t',  ['id' => 'object_id']);
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }

    public function getOrderTypes()
    {

        return $this->hasOne(OrderType::className(), ['id' => 'order_type_id']);//hasOne、hasMany是Yii2特有的用于多表关联查询的函数
        // return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as d',  ['id' => 'region_id']);
    }


    public function getOrderResumes(){
        return $this->hasOne(OrderResumes::className(), ['order_id' => 'id']);
    }

    public function getHistoryOrderResumes(){
        return $this->hasOne(OrderResumes::className(), ['order_id' => 'id'])->select(['order_id', 'count(*) as countOrder'])->groupBy(['order_id']);
    }



    public function getRegions()
    {
       // return $this->hasOne(Region::className(), ['region_id' => 'parent_region_id'])->viaTable(Region::className() . 'as c',  ['id' => 'region_id']);
        return $this->hasOne(Region::className(), ['id' => 'region_id']);
    }

    public function getParentRegions()
    {
        return $this->hasOne(Region::className(), ['id' => 'parent_region_id'])->viaTable(Region::tableName() . 'as df',  ['id' => 'region_id']);
    }

    public function getExternalAttrs()
    {
        /**
         * 第一个参数为要关联的字表模型类名称，
         *第二个参数指定 通过子表的 customer_id 去关联主表的 id 字段
         */
        return $this->hasMany(OrderExternalAttr::className(), ['order_id' => 'id']);

    }
    public function getExternalAttrs2()
    {

        $where['attr_spec_id'] = 1;
        return $this->hasOne(OrderExternalAttr::className(), ['order_id' => 'id'])->where($where);

    }



    public function getCityList($parent_region_id=null)
    {
        $model = Region::find()->where(['parent_region_id' => $parent_region_id ])->orderBy('id')->all();


        return ArrayHelper::map($model, 'id', 'region_name');

    }

    public function getLocalAttrsInsts()
    {

        return $this->localAttrsInsts;
    }

    public function getAttrSpecs()
    {

        return $this->attrSpecs;
    }


    // 根据订单上的对象标识查询cust属性表的号码字段
    public function getCustExternalAttrs()
    {

        return $this->hasOne(CustExternalAttr::className(), ['cust_id' => 'object_id']);

    }
}

?>
